# 自动取款机的存取款模拟

# 定义一个用于存放账户信息的列表变量
global accountlist
accountlist = [{'account_name': 'zhangsan', 'password': '123456', 'account_money': 100.00},
                      {'account_name': 'lisi', 'password': '000000', 'account_money': 1000.00},
                      {'account_name': 'wangwu', 'password': '111111', 'account_money': 2000.00}]

def pdaccount(account1, password1):
    '''
    判断输入的账号和密码是否在总账户信息里面，如果为真返回索引位置，否则返回-1
    '''
    for i in range(len(accountlist)):
        if (account1==accountlist[i]['account_name'] and password1==accountlist[i]['password']):
            print("登录系统成功！")
            break
        elif i==len(accountlist)-1 and (account1!=accountlist[i]['account_name'] or password1!=accountlist[i]['password']):
            print("输入的账号或密码错误，请重新输入：")
            i = -1
    return i

def ShowMoney(i):
    '''
    显示总账户列表中索引为i的元素的余额
    '''
    print('当前账户余额为',accountlist[i]['account_money'])

def QkMoney(i,x):
    '''
    从总账户列表中索引为i的元素中取款x
    '''
    if x <= accountlist[i]['account_money']:
        accountlist[i]['account_money'] -= x
        result = accountlist[i]['account_money']
        print("取款成功，当前账户余额为{:.2f}".format(accountlist[i]['account_money']))
    else:
        print("取款数不得超过当前账户余额，当前账户余额为",accountlist[i]['account_money'],',请重新输入:')
        result = accountlist[i]['account_money'] - x
    return result

def CkMoney(i,x):
    '''
    向总账户列表中索引为i的元素中存款x
    '''
    accountlist[i]['account_money'] += x
    print("存款成功，当前账户余额为{:.2f}".format(accountlist[i]['account_money']))


while True:
    # 输出初始界面
    print("=" * 12, "自动存取款机存取款系统", "=" * 14)
    print("{0:1} {1:13}".format(" ", "1. 登录系统"))
    print("{0:1} {1:13}".format(" ", "2. 查询余额"))
    print("{0:1} {1:13}".format(" ", "3. 取钱"))
    print("{0:1} {1:13}".format(" ", "4. 存钱"))
    print("{0:1} {1:13}".format(" ", "5. 退出系统"))
    print("=" * 40)
    key = input("请输入对应的选择：")
    # 根据键盘值，判断并执行对应的操作
    if key == "1":
        print("=" * 12, "登录系统", "=" * 14)
        account2 = input("请输入您的账号：")
        password2 = input("请输入您的密码：")
        flag = pdaccount(account2, password2)
        while flag == -1:
            account2 = input("请输入您的账号：")
            password2 = input("请输入您的密码：")
            flag = pdaccount(account2, password2)
        else:
            key = input("查询余额请按2，取款请按3，存钱请按4，退出请按5，请选择：")
            while key != "5":
                if key == "2":
                    ShowMoney(flag)
                    key = input("查询余额请按2，取款请按3，存钱请按4，退出请按5，请选择：")
                elif key == "3":
                    qk_money = float(input("请输入您的取款金额："))
                    tmp = QkMoney(flag, qk_money)
                    while tmp < 0:
                        qk_money = float(input("请输入您的取款金额："))
                        tmp = QkMoney(flag, qk_money)
                    key = input("查询余额请按2，取款请按3，存钱请按4，退出请按5，请选择：")
                elif key == "4":
                    ck_money = float(input("请输入您的存款金额："))
                    CkMoney(flag, ck_money)
                    key = input("查询余额请按2，取款请按3，存钱请按4，退出请按5，请选择：")
                else:
                    print("======== 无效的键盘输入！ ==========")
            else:
                print("=" * 12, "退出系统成功！", "=" * 14)
                break
    else:
        print("请先登录系统再执行后续操作！")


        #input("按回车键继续：")

